// pages/houseSearch/houseSearch.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    httpApiImg: app.globalData.httpApiImg,
    showCoverInp: true,
    hasData: true,
    totalPrice: '总价',
    room: '户型',
    area: '面积',
    priceData: [
      { id: 0, name: '不限' },
      { id: 1, name: '40万以下' },
      { id: 2, name: '40-60万' },
      { id: 3, name: '60-90万' },
      { id: 4, name: '90-120万' },
      { id: 5, name: '120-150万' },
      { id: 6, name: '150-200万' },
      { id: 7, name: '200-300万' },
      { id: 8, name: '300-500万' },
      { id: 9, name: '500万以上' }
    ],
    houseData: [
      { id: 0, name: '不限' },
      { id: 1, name: '一室' },
      { id: 2, name: '二室' },
      { id: 3, name: '三室' },
      { id: 4, name: '四室' },
      { id: 5, name: '五室' },
      { id: 6, name: '五室以上' }
    ],
    areaData: [
      { id: 0, name: '不限' },
      { id: 1, name: '40㎡以下' },
      { id: 2, name: '40-60㎡' },
      { id: 3, name: '60-80㎡' },
      { id: 4, name: '80-100㎡' },
      { id: 5, name: '100-120㎡' },
      { id: 6, name: '120-140㎡' },
      { id: 7, name: '140-200㎡' },
      { id: 8, name: '200-400㎡' },
      { id: 9, name: '400㎡以上' }
    ],
    sortTypeData: [
      { value: '默认排序', isCheck: true },
      { value: '最新发布', isCheck: false },
      { value: '单价', isCheck: false, hasSort: true },
      { value: '总价', isCheck: false, hasSort: true },
      { value: '面积', isCheck: false, hasSort: true },
    ],
    houseFlag:'',
    submitData: {
      orderType: 1, //	排序类型			
      houseType: '', //	房源类型（1：新房 2：二手房）			
      sortType: 1, //	升降序（1：降序 2：升序）	
      communityName: '', //	小区名			
      location: '', //	位置	
      region: '', //	区域			
      subway: '', //	地铁			
      schoolDistrict: '', //	学区			
      totalPrice: '', //	总价	
      min: '', //	最低价	
      max: '', //	最高价	
      room: '', //	户型	
      houseSourceTypeId: '', //	房源类型（1：住宅 2：别墅 3：写字楼 4：商铺 5：厂房仓库 6：车库车位）	
      area: '', //	面积	
      decorate: '', //	装修			
      year: '', //	房龄	
      floor: '', //	楼层	
      toward: '', //	朝向			
      taxFee: '', //	税费
      propertyRight: '', //	产权
      pageNumber: 10,
      currentPage: 1
    },
    commName:'',
    houseList: [],
    hasData: true,
    loadSuccess: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading()
    this.setData({
      ['submitData.houseType']: options.flag,
      houseFlag: options.flag,
      ['submitData.communityName']: options.communityName
    })
    wx.setNavigationBarTitle({
      title: options.communityName
    })
    this.getHalfHouse()
  },
  inputFocus: function (e) {
    this.setData({
      showCoverInp:false
    })
  },
  inputBlur: function (e) {
    if (this.data.commName == ''){
      this.setData({
        showCoverInp: true
      })
    }
  },
  changeInput: function(e){
    this.setData({
      ['submitData.currentPage']: 1,
      houseList: [],
      commName: e.detail.value,
      ['submitData.communityName']: e.detail.value
    })
    this.getHalfHouse()
  },
  // 获取二手房所有的数据
  getHalfHouse: function () {
    var _this = this;
    wx.request({
      url: app.globalData.httpApiCar+ 'tGyHouseSale/getHouseInfosWithParamsByPage',
      data: this.data.submitData,
      success: function (res) {
        _this.setData({
          loadSuccess: true
        })
        wx.hideLoading()
        if (res.data.success) {
          wx.hideNavigationBarLoading() //完成停止加载
          wx.stopPullDownRefresh() //停止下拉刷新
          var houses = res.data.data.houses;
          for (var i = 0; i < houses.length; i++) {
            if (houses[i].picture) {
              houses[i].picture = houses[i].picture.split(",");
            }
            if (houses[i].housingCharacteristics) {
              houses[i].housingCharacteristics = houses[i].housingCharacteristics.split(",");
            }
            _this.data.houseList.push(houses[i])
          }
          _this.setData({
            houseList: _this.data.houseList
          })
          if (_this.data.houseList.length == res.data.data.page.totalNumber) {
            _this.setData({
              hasData: false
            })
          }
        }
      }
    })
  },
  // 更改排序方式
  changeSort: function(e){
    var index = e.currentTarget.dataset.index;
    if (index > 1 && this.data.sortTypeData[index].isCheck) {
      this.data.sortTypeData[index].isUp = !this.data.sortTypeData[index].isUp;
      if (this.data.sortTypeData[index].isUp) {
        this.setData({
          ['submitData.sortType']: 2
        })
      }
      else {
        this.setData({
          ['submitData.sortType']: 1
        })
      }
    }

    for (var i = 0; i < this.data.sortTypeData.length; i++) {
      this.data.sortTypeData[i].isCheck = false;
    }
    this.data.sortTypeData[index].isCheck = true;
    this.setData({
      ['submitData.currentPage']: 1,
      houseList: [],
      ['submitData.orderType']: index + 1,
      sortTypeData: this.data.sortTypeData
    })
    this.getHalfHouse()
  },

  // 显示隐藏类型下拉选项
  typeTap: function (e) {
    var tag = e.currentTarget.dataset.tag;
    this.hideMask(tag, this.data[tag]);
    this.setData({
      currentFirId: 0,
      [tag]: !this.data[tag]
    })
  },
  // 隐藏所有的弹出框
  hideMask: function (tag, flag) {
    this.setData({
      typeAreaShow: false,
      typePriceShow: false,
      typeHouseShow: false
    })
    if (tag) {
      this.setData({
        [tag]: flag
      })
    }

  },
  // 禁止冒泡
  catchFunc: function () {

  },
  // 选中菜单
  selectType: function (e) {
    var item = e.currentTarget.dataset.item;
    var tag = e.currentTarget.dataset.tag;
    var show = e.currentTarget.dataset.show;
    var key = e.currentTarget.dataset.key;
    this.setData({
      ['submitData.currentPage']: 1,
      houseList: [],
      ['submitData.' + key]: item.id,
      currentSecId: item.id,
      [tag]: false,
      [show]: item.name
    })
    if (item.name == '不限') {
      var name;
      if (show == 'area') {
        name = '面积'
      }
      else if (show == 'totalPrice') {
        name = '总价'
      }
      else if (show == 'room') {
        name = '户型'
      }
      this.setData({
        [show]: name
      })
    }
    this.getHalfHouse()
  },
  // 进入详情页
  enterDetail: function (e) {
    var flag = e.currentTarget.dataset.flag;
    var item = e.currentTarget.dataset.item;
    wx.navigateTo({
      url: '/pages/houseDetail/houseDetail?flag=' + flag + '&name=' + item.title + '&id=' + item.id + '&communityName=' + item.communityName
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading();
    this.setData({
      ['submitData.currentPage']: 1,
      houseList: []
    })
    this.getHalfHouse();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.setData({
      ['submitData.currentPage']: this.data.submitData.currentPage + 1
    })
    this.getHalfHouse();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})